package com.gmadmin.configuration;

import com.gmadmin.websocket.handler.CustomWsHandler;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.socket.config.annotation.EnableWebSocket;
import org.springframework.web.socket.config.annotation.WebSocketConfigurer;
import org.springframework.web.socket.config.annotation.WebSocketHandlerRegistry;

/**
 * @author zhangliang
 * @version 1.0
 * @date 2022/8/1 18:50
 */
@Configuration
@EnableWebSocket
public class WebSocketConfig implements WebSocketConfigurer {

    private final CustomWsHandler customWsHandler;

    public WebSocketConfig(CustomWsHandler customWsHandler) {
        this.customWsHandler = customWsHandler;
    }

    /**
     * 注册WebSocketHandler（WebSocket的核心处理器，可以监听指定端点），也可以设置SockJS回退选项
     */
    @Override
    public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
        registry
                .addHandler(customWsHandler, "/websocket/system/notification")
                //允许跨域
                .setAllowedOrigins("*");
    }
}
